/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/time.h"

/**
 * @brief Format a time structure into a string according to a given format.
 *
 * @param str The buffer to store the formatted time string.
 * @param maxsize The maximum size of the buffer.
 * @param format The format string for the time representation.
 * @param timeptr Pointer to a Ttm structure containing the time to format.
 * @return The length of the formatted string.
 */
size_t Sstrftime(char *str, size_t maxsize, const char *format, const struct Ttm *timeptr) {
    int len = 0;
    len += snprintf(str + len, maxsize - len, "%d-%02d-%02d", 1900 + timeptr->tm_year, timeptr->tm_mon + 1, timeptr->tm_mday);
    if (len < maxsize) {
        str[len++] = ' ';
    }
    len += snprintf(str + len, maxsize - len, "%02d:%02d:%02d", timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
    return len;
}
